package knowse.app.daos;

import knowse.app.KnowSEApp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper{
	private static final String DB_NAME 	= "knowseDB";
	private static final int 	DB_VERSION 	= 1;
		
	public DBHelper(){
		super(KnowSEApp.getContext(), DB_NAME, null, DB_VERSION);
	}
		
	/*
	 * TODO
	 * 		Foreign key support : PRAGMA ON 
	 * 
	 * NOTE! SQLite with version prior to 3.2.19 does not support
	 * 		 FOREIGN KEY. .
	 */
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		/*
		 * Create table for Project
		 *   
		 *   CREATE TABLE Project
		 * 	   Name TEXT PRIMARY KEY;
		 * 
		 */
		String project = 
				"CREATE TABLE " + ProjectDAO.TABLE + "(" +
					ProjectDAO._NAME + " TEXT PRIMARY KEY" +
					");"
				;
		db.execSQL(project);
		
		/*
		 * Create table for TestStatus
		 *   
		 *   CREATE TABLE TestStatus(
		 * 	   _StartTime INTEGER PRIMARY KEY,
		 * 	   _StartDate INTEGER PRINARY KEY,
		 * 		NumTests  Integer,
		 * 		NumSkips  Integer,
		 * 		NumAssertions Integer,
		 * 		NumFailures Integer,
		 * 		NumErrors Integer,
		 * 		ElapsedTime Integer,
		 * 		ProjectName TEXT,
		 *      FOREIGN KEY(ProjectName) references Project(_Name),
		 */
		 String testStatus =
			"CREATE TABLE " + TestStatusDAO.Table + "(" +
						TestStatusDAO.StartTime + " TEXT"
						+ "," +
						TestStatusDAO.StartDate + " TEXT"
						+ "," +
						TestStatusDAO.NumTests + " INTEGER"
						+ "," +
						TestStatusDAO.NumSkips + " INTEGER"
						+ "," +
						TestStatusDAO.NumAssertions + " INTEGER"
						+ "," +
						TestStatusDAO.NumFailures + " INTEGER"
						+ "," +
						TestStatusDAO.NumErrors + " INTEGER"
						+ "," +
						TestStatusDAO.ElapsedTime + " INTEGER"
						+ "," +
						TestStatusDAO.ProjectName + " TEXT"
						+ "," +
						TestStatusDAO.TimeStamp + " INTEGER PRIMARY KEY"
						+ "," +
						"FOREIGN KEY(" +
							TestStatusDAO.ProjectName + ") " + " REFERENCES " +
							ProjectDAO.TABLE + "(" + 
								ProjectDAO._NAME +
								")"
								+
							");"
			;
		 db.execSQL(testStatus);
		 
			/*
			 * Create table for TestStatusDetail
			 *   
			 *   CREATE TABLE TestStatusDetail(
			 * 	   _ID INTEGER PRIMARY KEY,
			 * 	   location TEXT,
			 * 		class TEXT,
			 * 		method TEXT,
			 * 		message TEXT
			 * 		type TEXT
			 *      FOREIGN KEY(ProjectName) references Project(_Name),
			 */
		 String testStatusDetail =
			"CREATE TABLE " + TestStatusDetailDAO.Table + "(" +
						TestStatusDetailDAO.Location + " TEXT"
						+ "," +
						TestStatusDetailDAO.Class_ + " TEXT"
						+ "," +
						TestStatusDetailDAO.Method + " TEXT"
						+ "," +
						TestStatusDetailDAO.Message + " TEXT"
						+ "," +
						TestStatusDetailDAO.Type + " TEXT"
						+ "," +
						TestStatusDetailDAO.ID + " INTEGER"
						+ "," +
						TestStatusDetailDAO.TestStatusTimeStamp + " INTEGER"
						+ "," +	
						TestStatusDetailDAO.Backtrace + " TEXT"
						+ "," +							
						"PRIMARY KEY (" +
							TestStatusDetailDAO.ID + 
							")"
						+ "," +
						"FOREIGN KEY(" +
							TestStatusDetailDAO.TestStatusTimeStamp + ") " + " REFERENCES " +
							TestStatusDAO.Table + "(" + 
								TestStatusDAO.TimeStamp +
								")"
								+
							");"
			;
		 db.execSQL(testStatusDetail);
		 
		 /*
			 * Create table for Log
			 *   
			 *   CREATE TABLE Log
			 *   	LogID Integer PRIMARY KEY
			 * 	  	json_class TEXT
			 * 		GitRepPath TEXT
			 * 		TimeStamp TEXT
			 * 
			 */
			String log = 
					"CREATE TABLE " + LogDAO.TABLE + "(" +
						LogDAO.LogID + " Integer PRIMARY KEY," +
						LogDAO.json_class + " TEXT" + "," +
						LogDAO.GitRepPath + " TEXT" + "," +
						LogDAO.TimeStamp + " TEXT" +
						");"
					;
			db.execSQL(log);

			 /*
			 * Create table for Commit
			 *   
			 *   CREATE TABLE Commit
			 *   	CommitID Integer PRIMARY KEY
			 * 	  	json_class TEXT
			 * 		AuthorName TEXT
			 * 		StartTime TEXT
			 * 		StartDate TEXT
			 * 		CommitHash TEXT
			 * 		ShortCommitHash TEXT
			 * 		LogID	Integer
			 * 		FOREIGN KEY(LogID) references Log(LogID)
			 * 		
			 * 		
			 */
			String commit = 
					"CREATE TABLE " + CommitDAO.Table + "(" +
						CommitDAO.CommitID + " Integer" + "," +
						CommitDAO.json_class + " TEXT" + "," +
						CommitDAO.AuthorName + " TEXT" + "," +
						CommitDAO.StartTime + " TEXT" + "," +
						CommitDAO.StartDate + " TEXT" + "," +
						CommitDAO.CommitHash + " TEXT" + "," +
						CommitDAO.ShortCommitHash + " TEXT" + "," +
						CommitDAO.LogID + " Integer" + "," +
						"PRIMARY KEY (" +
						CommitDAO.CommitID + 
						")"
						+ "," +
						"FOREIGN KEY(" +
						CommitDAO.LogID + ") " + " REFERENCES " +
						LogDAO.TABLE + "(" + 
							LogDAO.LogID +
							")"
							+
						");"
					;
			db.execSQL(commit);
			
			
			 /*
			 * Create table for Change
			 *   
			 *   CREATE TABLE Commit
			 *   	ChangeID Integer PRIMARY KEY
			 * 	  	JsonClass TEXT
			 * 		FileDir TEXT
			 * 		FileName TEXT
			 * 		IsTextFile Integer
			 * 		IsBinaryFile Integer
			 * 		NumChangedLines Integer
			 * 		NumAddedLines	Integer
			 * 		NumDeletedLines Integer
			 * 		NumBytesBefore	Integer
			 * 		NumBytesAfter	Integer
			 * 		CommitID	Integer
			 * 		FOREIGN KEY(CommitID) references Commit(CommitID)
			 * 		
			 * 		
			 */
			String change = 
					"CREATE TABLE " + ChangeDAO.TABLE + "(" +
						ChangeDAO.ChangeID + " Integer" + "," +
						ChangeDAO.JsonClass + " TEXT" + "," +
						ChangeDAO.FileDir + " TEXT" + "," +
						ChangeDAO.FileName + " TEXT" + "," +
						ChangeDAO.IsTextFile + " Integer" + "," +
						ChangeDAO.IsBinaryFile + " Integer" + "," +
						ChangeDAO.NumChangedLines + " Integer" + "," +
						ChangeDAO.NumAddedLines + " Integer" + "," +
						ChangeDAO.NumDeletedLines + " Integer" + "," +
						ChangeDAO.NumBytesBefore + " Integer" + "," +
						ChangeDAO.NumBytesAfter + " Integer" + "," +
						ChangeDAO.CommitID+ " Integer" + "," +
						"PRIMARY KEY (" +
						ChangeDAO.ChangeID + 
						")"
						+ "," +
						"FOREIGN KEY(" +
						ChangeDAO.CommitID + ") " + " REFERENCES " +
						CommitDAO.Table + "(" + 
							CommitDAO.CommitID +
							")"
							+
						");"
					;
			db.execSQL(change);
		 
		 
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	
}
